package com.excesys.exsecs.access.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuzhikun
 * @title: BeanMapUtil
 * @projectName pig
 * @description: Map和对象互转
 * @date 2023/5/911:48
 */
public class BeanMapUtil {

	/***
	 * 把Map转为对象
	 * @param source source
	 * @param targetClass target
	 * @param <T> 返回值类型
	 * @return 返回值
	 * @throws Exception newInstance可能会抛出的异常
	 */
	public static <T> T mapToBean(Map source, Class<T> targetClass) {

		T object = JSONUtil.toBean(JSONUtil.toJsonStr(source), targetClass);

		return object;
	}

	/**
	 * 对象转Map
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> objectToMap(Object obj) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fields = obj.getClass().getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			map.put(field.getName(), field.get(obj));
		}
		return map;
	}

	/***
	 * List集合转List<Map<String, Object>> List<T> --> List<Map<String, Object>>
	 * @param objectList
	 * @param <T>
	 * @return
	 * @throws Exception
	 */
	public static <T> List<Map<String, Object>> objectListToListMap(List<T> objectList) throws Exception {
		ArrayList<Map<String, Object>> resultList = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		for (T t : objectList) {
			resultList.add(objectToMap(t));
		}
		return resultList;
	}

	/**
	 * List集合转List<Map<String, Object>>
	 * @param objectList
	 * @param keyName
	 * @param <T>
	 * @return
	 * @throws Exception
	 */
	public static <T> Map<String, List<Object>> objectListToMapList(List<T> objectList, String[] keyName)
			throws Exception {
		Map<String, List<Object>> resultMap = new HashMap<>();
		for (int i = 0; i < keyName.length; i++) {
			List<Object> arrayList = new ArrayList<>();
			for (T t : objectList) {// List有序，所以对每个对象依次变为map,然后得到对应的值，存入arrayList
				arrayList.add(objectToMap(t).get(keyName[i]));
			}
			resultMap.put(keyName[i], arrayList);// 将keyName和对应List集合存入resultMap
		}
		return resultMap;
	}

	/**
	 * 把source对象指定类型的字段的值重置为指定值
	 * @param sourceBean 源对象
	 * @param typeName 目标字段类型：java.lang.String,java.lang.Long
	 * @param filedValue 条件值
	 * @param newValue 重置的新值
	 * @param <T> 返回值类型
	 * @return 返回值
	 * @throws Exception newInstance可能会抛出的异常
	 */
	public static <T> T beanSetValue(T sourceBean, String typeName, Object filedValue, Object newValue)
			throws Exception {

		Field[] fields = sourceBean.getClass().getDeclaredFields();
		for (Field field : fields) {
			int mod = field.getModifiers();
			if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
				continue;
			}
			field.setAccessible(true);
			Object fieldVal = field.get(sourceBean);
			if (field.getType().getName().equals(typeName) && ObjectUtil.isNotNull(fieldVal)
					&& fieldVal.equals(filedValue)) {
				field.set(sourceBean, newValue);
			}
		}
		return sourceBean;
	}

}
